// ScaleStrategy.java
// by Alexandre Martins, 2013
// MAC 5857 - Prof. Leonidas

package example.ilm.model;

import ilm.framework.assignment.model.DomainAction;

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.HashMap;

import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;

public class ScaleStrategy implements DragnDropStrategy
{
    private Shape shape;
    private double unitLength;
    private double initialScale;

    public ScaleStrategy(Shape shape, int x, int y)
    {
        this.shape = shape;
        initialScale = shape.getScale();
        unitLength = (new Point2D.Double(x, y)).distance(shape.getPosition());
        if(unitLength < 1)
            unitLength = 1;
    }

    public void drag(int x, int y)
    {
        Point2D delta = new Point2D.Double(
            x - shape.getPosition().getX(),
            y - shape.getPosition().getY()
        );
        double magnitude = delta.distance(0, 0);
        shape.setScale(initialScale * magnitude / unitLength);
    }

    public void drop(HashMap theDomainActions)
    {
        ScaleShape action = (ScaleShape)theDomainActions.get("scaleShape");
        action.setShape(shape);
        action.scale(shape.getScale() / initialScale);
        shape.setScale(initialScale);
        action.execute();
    }
    
    public GeometricElement getGeometricElement()
    {
    	return shape;
    }
}
